﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;

namespace zielonka.co.uk.extensions.system.directoryservices
{
    // Description:	extensions that work with SearchResult to retrieve its data.
    /*
        result.GetValue("sAMAccountName", String.Empty)
     */

    public static partial class SearchResultExtensions
    {
        public static IEnumerable<T> GetEnumerable<T>(this SearchResult result, string key)
        {
            if (!result.Properties.Contains(key))
            {
                yield break;
            }

            var values = result.Properties[key];


            foreach (var value in values)
            {
                var valueCollection = value as T[];
                if (valueCollection != null)
                {
                    foreach (var innerValue in valueCollection)
                    {
                        yield return innerValue;
                    }
                    yield break;
                }
                yield return (T)value;
            }
        }
    }
}
